<?php

namespace app\common\model\red;

use app\common\model\User;
use think\Model;


class Log extends Model
{





    // 表名
    protected $name = 'red_log';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'invalidtime_text',
        'day',
        'status_text',
        'state_text'
    ];

    public function getDayAttr($value, $data)
    {
        return diff_date(time(), $data['invalidtime']);
    }

    public function getStatusList()
    {
        return ['normal' => __('Normal'), 'hidden' => __('Hidden')];
    }

    public function getStateList()
    {
        return ['0' => __('State 0'), '1' => __('State 1')];
    }


    public function getInvalidtimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['invalidtime']) ? $data['invalidtime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getStateTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['state']) ? $data['state'] : '');
        $list = $this->getStateList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    protected function setInvalidtimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


    public function user()
    {
        return $this->belongsTo('app\common\model\User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function envelope()
    {
        return $this->belongsTo('Envelope', 'red_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function addLog($data)
    {
        $count = self::where('red_id', $data->id)->where('user_id', $data->user_id)->where('state', '1')->count();
        if ($count >= $data->num) {
            exception('购买量已达上限');
        }
        $user = User::get($data->user_id);
        if ($user->score <= $data->integral) {
            exception('积分不足');
        }
        $res = self::create([
            'user_id' => $data->user_id,
            'sn' => sn('red_log', 'sn', 'LO'),
            'red_id' => $data->id,
            'invalidtime' => strtotime("+30 day"),
            'efficiency' => $data->efficiency,
            'integral' => $data->integral,
            'paytime' => time(),
        ]);
        if ($res) {
            return $res;
        } else {
            exception('下单失败');
        }
    }
    public static function red_envelope()
    {
        $list = self::all(['status' => 'normal', 'state' => '1']);
        foreach ($list as $v) {
            if ($v->invalidtime < time()) {
                $v->save(['status' => 'hidden']);
            }
        }
    }
}
